/*
Automatic clinic tests using a robot arm.
Copyright (C) 2010  CIC-CPC, CHU Gabriel Montpied
All other trademarks, logos and copyrights are the property
of their respective owners.

This file is part of cpc-painbot.

cpc-painbot is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

cpc-painbot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with cpc-painbot.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QLibraryInfo>
#include <QTranslator>
#include <QActionGroup>
#include <QMessageBox>
#include <QDebug>

#include "indicateurprocess.h"

#include "assistantwindup.h"
#include "assistantupanddown.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    trans_qt_(new QTranslator(this)),
    trans_painbot_(new QTranslator(this))

{
    ui->setupUi(this);
    construireLangues();
    progs_["Wind Up"] = new AssistantWindUp(ui->tabBras->bras(),
                                            ui->widgetIndicateur2->indicateur(),
                                            this);
    progs_["Up and Down"] = new AssistantUpAndDown(this);

    connect(ui->actionA_propos_Qt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));

    changerLangue(QLocale::system().name());
    ui->widgetIndicateur1->indicateur()->setAdresse(1);
    ui->widgetIndicateur2->indicateur()->setAdresse(1);

    // Génération de la liste des programmes.
    foreach (QString prog, progs_.keys())
        ui->progsListWidget->addItem(prog);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MainWindow::lancerProgramme(QListWidgetItem * prog)
{
    if ( progs_.contains(prog->text()) ) {
        progs_[prog->text()]->restart();
        progs_[prog->text()]->show();
    }
}

void MainWindow::changerLangue(QAction *langue)
{
    changerLangue(langue->data().toString());
}

void MainWindow::changerLangue(const QString &locale)
{
    // Installation de la langue par défaut (celle du système)
    // Eléments propres à Qt.
    trans_qt_->load("qt_" + locale,
                    QLibraryInfo::location(QLibraryInfo::TranslationsPath));
    // Eléments propres à l'application.
    trans_painbot_->load("painbot_" + locale);

    // Installation des traducteurs.
    qApp->installTranslator(trans_qt_);
    qApp->installTranslator(trans_painbot_);
}

void MainWindow::aPropos()
{
    QMessageBox::about(this,
        tr("A propos de %1").arg(QCoreApplication::applicationName()),
        tr("<p>Copyright &copy; 2010  <a href=\"http://cic-cpc.chu-clermontferrand.fr/\">CIC-CPC</a>,"
            " <a href=\"http://www.chu-clermontferrand.fr/\">CHU Gabriel Montpied</a>.<br />"
            "This Software is released under the terms of the"
            " Lesser General Public License (LGPL) version 3 or later.<br />"
            "<em>All other trademarks, logos and copyrights are the property"
            " of their respective owners</em>.</p>"
            "<p>Created by"
            " <a href=\"http://www.linkedin.com/in/gregoireastruc\">Gr&eacute;goire Astruc</a>"
            " &lt;< a href=\"mailto:gregoire.astruc@anelis.isima.fr\">"
            "gregoire.astruc@anelis.isima.fr</a>&gt; on his 2nd year internship at"
            " <a href=\"http://www.isima.fr/\">ISIMA</a> <em>(engineering school in computing)</em>.</p>"
            "<p><a href=\"http://code.google.com/p/%1\">%2 project page</a>.<br />"
            "If you find any problems, bugs or have any suggestions,"
            " please file an issue on the project website.</p>"
            "<p style=\"vertical-align: middle;\" style=\"margin-left:auto;margin-right:auto;\">"
            "<a href=\"http://www.bitrock.com/\"><img src=\":/images/bitrock\"/></a>"
            "<a href=\"http://www.gnu.org/licenses/lgpl.html\"><img src=\":/images/lgplv3-grand\"/></a></p>"
        ).arg(QCoreApplication::applicationName().toLower())
        .arg(QCoreApplication::applicationName())
    );
}

void MainWindow::construireLangues()
{
    QActionGroup *langues = new QActionGroup(this);
    langues->setExclusive(true);

    langues->addAction(creerLangue(QLocale::French, langues));
    langues->addAction(creerLangue(QLocale::English, langues));

    ui->menu_Langue->addActions(langues->actions());

    connect(langues, SIGNAL(triggered(QAction*)), this, SLOT(changerLangue(QAction*)));
}

QAction *MainWindow::creerLangue(const QLocale &l, QObject *parent)
{
    QAction *action = new QAction(parent);
    QString langue = QLocale::languageToString(l.language());
    QString pays = QLocale::countryToString(l.country());

    action->setCheckable(true);
    action->setChecked(QLocale::system().name() == l.name());
    action->setText(tr("%1 (%2)").arg(langue).arg(pays));
    action->setData(l.name());
    action->setIcon(QIcon(":/flags/" + l.name().split('_').at(1).toLower()));

    return action;
}


